package app.netant.book.sjxs;

import java.io.File;

import cn.lily.com.db.DBFactory;
import cn.lily.com.file.FilePath;
import cn.lily.com.lang.number.MathUtil;
import cn.lily.inst.esb.control.autorun.RunCacheFilter;

/**
 * 久久实体类
 */
public class SjxsEntity {
	
	private String novelID = System.currentTimeMillis() + MathUtil.random(3);
	private String novelName = "";	
	private String novelFullName = "";
	private String novelUrl = "";	
	private String novelMemo = "";
	private String novelDown = "0";
	private String novelWriter = "";
	private String novelDate = "";
	private String novelSize = "";
	private String novelState = "";
	
	public String getNovelID() {
		return novelID;
	}
	public String getNovelName() {
		return novelName;
	}
	public String getNovelFullName() {
		return novelFullName;
	}
	public String getNovelUrl() {
		return novelUrl;
	}
	public String getNovelMemo() {
		return novelMemo;
	}
	public String getNovelDown() {
		return novelDown;
	}
	public String getNovelWriter() {
		return novelWriter;
	}
	public String getNovelDate() {
		return novelDate;
	}
	public String getNovelSize() {
		return novelSize;
	}
	public String getNovelState() {
		return novelState;
	}
	public String getNovelPath() {
		return novelPath;
	}
	public String getNovelType() {
		return novelType;
	}
	public String getDownPath() {
		return downPath;
	}

	private String novelPath = "";
	private String novelType = "奇幻修真";
	private String downPath = "";
	
	public void setNovelID(String novelID) {
		this.novelID = novelID;
	}
	public void setNovelName(String novelName) {
		this.novelName = this.getString(novelName);
	}
	public void setNovelFullName(String novelFullName){
		this.novelFullName = this.getString(novelFullName);
	}
	public void setNovelUrl(String novelUrl) {
		this.novelUrl = this.getString(novelUrl);
	}
	public void setNovelMemo(String novelMemo) {
		this.novelMemo = this.getString(novelMemo);
		if(this.novelMemo.length()>1300){
			this.novelMemo = this.novelMemo.substring(0,1300)+"...";
		}
	}
	public void setNovelDown(String novelDown) {
		this.novelDown = this.getString(novelDown);
	}
	public void setNovelWriter(String novelWriter) {
		this.novelWriter = this.getString(novelWriter);
	}
	public void setNovelDate(String novelDate) {
		this.novelDate = this.getString(novelDate);
	}
	public void setNovelSize(String novelSize) {
		this.novelSize = this.getString(novelSize);
	}
	public void setNovelState(String novelState) {
		this.novelState = this.getString(novelState);
	}
	public void setNovelPath(String novelPath) {
		this.novelPath = this.getString(novelPath);
	}
	public void setNovelType(String novelType) {
		this.novelType = this.getString(novelType);
		this.novelType = this.novelType.replaceAll("\\\\", "_")
									   .replaceAll("/", "_")
									   .replaceAll(":", "：")
									   .replaceAll("\\?", "_")
									   .replaceAll("\\*", "_")
									   .replaceAll("<", "_")
									   .replaceAll(">", "_")
									   .replaceAll("\\|", "_");
	}
	public void setDownPath(String downPath) {
		this.downPath = downPath;
	}
	
	protected String getString(String src){
		
		if(src == null || "".equals(src)){
			return src;
		}
		
		src = src.trim();
		
		return src.replaceAll("'", "’")
				  .replaceAll("\"", "”")
				  .replaceAll("&nbsp;", "")
				  .replaceAll("<br>", "")
				  .replaceAll("<br/>", "")
				  .replaceAll("<p>", "")
				  .replaceAll("</p>", "");
	}
	
	public void setTag(String tagName,String tagValue){
		
		tagName = this.getString(tagName);
		tagValue = this.getString(tagValue);
		
		if("书籍分类".equals(tagName)){
			this.setNovelType(tagValue);
		}
		
		else if("连载状态".equals(tagName)){
			this.setNovelState(tagValue);
		}
		
		else if("书籍作者".equals(tagName)){
			this.setNovelWriter(tagValue);
		}
		
		else if("文件大小".equals(tagName)){
			this.setNovelSize(tagValue);
		}
		
		else if("更新日期".equals(tagName)){
			this.setNovelDate(tagValue);
		}
		
	}
	
	/**
	 * 文件下载
	 */
	public void download(){
		
		try {
			
			String folderName = FilePath.getWebRootPath("/file/txt/sjxs/"+this.novelType);
			File folder = new File(folderName);
			if(!folder.exists()){
				folder.mkdirs();
			}
			
			String url = this.downPath;
			
			System.out.println("开始下载："+this.novelName);
			
			String name = this.novelFullName.replaceAll("\\\\", "_")
					   .replaceAll("/", "_")
					   .replaceAll(":", "：")
					   .replaceAll("\\?", "_")
					   .replaceAll("\\*", "_")
					   .replaceAll("<", "_")
					   .replaceAll(">", "_")
					   .replaceAll("\\|", "_");
			
			SjxsDown down = new SjxsDown();
			down.setFilePath(folderName+"/"+name);
			down.setUrl(url);
			down.start();
			
			long startTime = System.currentTimeMillis();
			
			while(true){		
				
				if("1".equals(down.isDown())){
					this.novelPath = "/file/txt/sjxs/"+this.novelType + "/" + name;
					this.novelDown = "1";
					break;
				}
				if("2".equals(down.isDown())){
					throw new RuntimeException("文件下载失败！");
				}
				else{
					
					long endTime = System.currentTimeMillis();
					long minus = endTime - startTime;
					/*if(minus > 3 * 60 * 1000){
						
						try {
							
							System.out.println("强行关闭线程........");
							
							//解除阻塞
							//down.interrupt();
							
							//强关线程
							down.stopThread();	
						} catch (Exception e) {
							e.printStackTrace();
						}	
						
						
						
					}*/
					
					if(minus > 3 * 60 * 1000){
						
						System.out.println("强行废弃线程........");
						
						throw new RuntimeException("线程弃置处理............");
					}
					
				}
				
				Thread.sleep(1000);
				
			}
			
			System.out.println("下载完成："+this.novelName);
			
		} catch (Exception e) {
			e.printStackTrace();
			this.novelDown = "0";
			
			String name = this.novelFullName.replaceAll("\\", "_")
					   .replaceAll("/", "_")
					   .replaceAll(":", "：")
					   .replaceAll("\\?", "_")
					   .replaceAll("\\*", "_")
					   .replaceAll("<", "_")
					   .replaceAll(">", "_")
					   .replaceAll("\\|", "_");
			
			File file = new File(FilePath.getWebRootPath("/file/txt/sjxs/"+this.novelType + "/" + name));
			if(file.exists()){
				file.delete();
			}
			
			throw new RuntimeException("文件下载失败！");
			
		}
		
	}
	
	public void insert(){
		String insertSQL = "insert into ${schema}.NET_SJXS_INFO "
						+ "(NOVEL_ID,NOVEL_NAME,NOVEL_URL,NOVEL_MEMO,"
						+ "NOVEL_DOWN,NOVEL_WRITER,NOVEL_DATE,NOVEL_SIZE,"
						+ "NOVEL_STATE,NOVEL_PATH,NOVEL_TYPE) "
						+ "values "
						+ "('"+this.novelID+"','"+this.novelName+"','"+this.novelUrl+"','"+this.novelMemo+"',"
						+ "'"+this.novelDown+"','"+this.novelWriter+"','"+this.novelDate+"','"+this.novelSize+"',"
						+ "'"+this.novelState+"','"+this.novelPath+"','"+this.novelType+"') ";
		DBFactory.getThreadDB().executeUpdate(insertSQL);
	}
	
}
